// pages/mine/info/info.js

// jwt工具
const { getJwt, removeJwt } = require("../../../utils/jwt.js");
const { server, serverApi } = require("../../../utils/config.js");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        server: server,
        head: "",
        nickname: "",
        newNickName: "",
    },

    /**
     * 选择头像
     */
    chooseAvatarHandle(e) {
        let that = this;
        wx.showLoading({
            title: '请稍候',
        });
        wx.uploadFile({
            filePath: e.detail.avatarUrl,
            name: 'resFile',
            url: serverApi + "/user/head",
            header: {
                "Authorization": "Bearer " + getJwt()
            },
            success(res) {
                let resDate = JSON.parse(res.data);
                if (resDate.code === 200) {
                    that.$storeCommit("setHead", e.detail.avatarUrl);
                    wx.navigateBack();
                } else {
                    wx.showModal({
                        title: '失败',
                        content: resDate.message
                    });
                }
            },
            fail(err) {
                wx.showModal({
                    title: '失败',
                    content: '请检查网络连接'
                });
            },
            complete() {
                wx.hideLoading();
            }
        })
    },

    /**
     * 输入监听
     */
    nicknameInputHandle(res) {
        this.data.newNickName = res.detail.value;
    },

    /**
     * 用户选择微信昵称
     */
    selectWxNickNameHandle(res) {
        let that = this;
        if (!res.detail.pass) {
            wx.showToast({
                icon: "error",
                title: '昵称不合法',
            });
            return;
        }
        let newNickName = that.data.newNickName;
        if (newNickName !== that.data.nickname) {
            if (newNickName.length === 0 || newNickName.length > 10) {
                wx.showModal({
                    title: '提示',
                    content: '昵称应该1~10个字符'
                });
                return;
            }
            wx.fetch({
                    url: "/user/username",
                    method: "POST",
                    data: {
                        username: newNickName
                    }
                })
                .then(res => {
                    that.$storeCommit("setUserName", newNickName);
                    wx.navigateBack();
                })
                .catch(err => {
                    if (err.code === 401 || err.code === 304) {
                        // 登录失效
                        removeJwt();
                        wx.navigateBack();
                    }
                });
        }
    },

    /**
     * 获取用户信息
     */
    getUserInfo() {
        let that = this;
        wx.fetch({ url: "/user" })
            .then(res => {
                that.setData({
                    newNickName: res.data.username,
                });
                that.$storeCommit("setUserName", res.data.username);
                that.$storeCommit("setHead", res.data.head);
            })
            .catch(err => {
                if (err.code === 401 || err.code === 304) {
                    // 登录失效
                    removeJwt();
                }
            });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        that.$effect(() => {
            that.setData({
                nickname: that.$getStoreData().username,
                head: that.$getStoreData().head,
            })
        });
        that.getUserInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})